using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;

using BPMN.MainWindow;

namespace BPMN.MainWindow
{
    public class Lane : Figure
    {
        #region Fields
        Pool parentPool;
        FlowObjectList flowObjects = new FlowObjectList();
        List<ConnectingObject> connectingObjects = new List<ConnectingObject>();
        #endregion
        #region Properties
        public Pool ParentPool
        {
            get
            {
                return parentPool;
            }
            set
            {
                parentPool = value;
            }
        }
        public FlowObjectList FlowObjects
        {
            get
            {
                return flowObjects;
            }
            set
            {
                flowObjects = value;
            }
        }
        public List<ConnectingObject> ConnectiongObjects
        {
            get
            {
                return connectingObjects;
            }
            set
            {
                connectingObjects = value;
            }
        }
        #endregion
        public Lane(string name, Pool parentPool,PointF location,SizeF size)
        {
            this.Name = name;
            this.parentPool = parentPool;
            this.Location = location;
            this.Size = size;
            this.BoundingBox = new RectangleF(this.Location.X+Variables.PoolTitleWidth,this.Location.Y, this.Size.Width-Variables.PoolTitleWidth,this.Size.Height);
            this.Path = new GraphicsPath();
            this.Path.AddRectangle(this.BoundingBox);
            this.Region = new Region(this.Path);
        }
        public override void Draw(Graphics graphics, Pen pen, Brush brush, PointF offset)
        {
            if (this.State == FigureState.Selected)
            {
                graphics.FillRectangle(brush, offset.X + Location.X, offset.Y + Location.Y, Size.Width, Size.Height);
            }
            graphics.DrawRectangle(pen, offset.X + Location.X, offset.Y + Location.Y, Size.Width, Size.Height);
            //graphics.DrawRectangle(pen, offset.X + Location.X, offset.Y + Location.Y, Variables.PoolTitleWidth, Size.Height);
            string displayName = Name;
            float titleSize = graphics.MeasureString(displayName, Variables.PoolTitleFont).Width;
            if (Size.Height > 0)
            {
                if (titleSize > Size.Height - 10)
                {
                    float charWidth = (titleSize + 5) / Name.Length;
                    displayName = Name.Remove((int)(Name.Length - (titleSize - Size.Height + 10) / charWidth));
                    displayName += "...";
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Resize bug");
            }
            titleSize = graphics.MeasureString(displayName, Variables.PoolTitleFont).Width;
            graphics.RotateTransform(-90);
            graphics.DrawString(displayName, Variables.PoolTitleFont, Variables.PoolTitleBrush, new PointF(-Location.Y - (Size.Height + titleSize) / 2 - offset.Y, Location.X + offset.X));
            graphics.RotateTransform(90);
            foreach (FlowObject flowObject in flowObjects)
            {
                flowObject.Draw(graphics, pen, brush, offset);
            }
            foreach (ConnectingObject connectingObject in connectingObjects)
            {
                connectingObject.Draw(graphics, pen, brush, offset);
            }
        }

        public override void UpdateSomeParametersLocation(PointF oldLocation)
        {            
            PointF diffence=new PointF(this.Location.X-oldLocation.X,this.Location.Y-oldLocation.Y);
            foreach (FlowObject flowObject in flowObjects)
            {
                System.Diagnostics.Debug.WriteLine(diffence.ToString());
                flowObject.updateLocation(diffence);
            }
            UpdateSomeParametersSize(new SizeF());
        }
        public override void UpdateSomeParametersSize(SizeF oldSize)
        {
            this.BoundingBox = new RectangleF(this.Location.X + Variables.PoolTitleWidth, this.Location.Y, this.Size.Width - Variables.PoolTitleWidth, this.Size.Height);
            this.Path = new GraphicsPath();
            this.Path.AddRectangle(this.BoundingBox);
            this.Region = new Region(this.Path);
            ResizeBoundingBox = new RectangleF(Location.X + Size.Width / 2, Location.Y + Size.Height - 20, 20, 20);
        }
    }
}
